﻿using System;
using System.Collections.Generic;
using System.Linq;
using Entities;
using Entities.DataBase;
using Entities.DataBase.Obj;
using Entities.DataBase.Persistent;
using WebService.Service.Transfer;

namespace WebService.Service {

    public class SettingServiceX : AbstractService, ISettingServiceX {

        public List<Setting> GetSettings(string passport) {
            return Run(passport, token => Services.Locate<ISettingRepository>().GetAll());
        }

        public bool UpdateSetting(string passport, SettingKey key, string value) {
            return Run(passport, token => {
                Services.Locate<ISettingRepository>().Update(key, value);
                return true;
            });
        }

        public bool UpdateSettings(string passport, UpdateSettingsRequest request) {
            return Run(passport, token => {
                var keyValues = request.Settings
                                       .Select(item => new KeyValuePair<string,string>(item.Key,item.Value))
                                       .ToList();
                Services.Locate<ISettingRepository>().UpdateSettings(keyValues);
                return true;
            });
        }

    }
}
